<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

require_once 'eleontev/HTML/BoxMsg.php';


// we combine all functions to get msg
// just for simplifing life in future
class AppMsg
{
	
	
	function getCommonMsgFile($file_name) {
		$file         = APP_MSG_DIR . APP_LANG . '/' . $file_name;
		$file_default = APP_MSG_DIR . 'en/' . $file_name;
		
		return (file_exists($file)) ? $file : $file_default;
	}
	
	
	function getModuleMsgFile($module, $file_name) {
		return AppMsg::getCommonMsgFile($module . '/' . $file_name);
	}
	
	
	function getMsgs($file_name, $module = false, $section = false, $process_sections = 1) {
	
		$file = AppMsg::getCommonMsgFile($file_name);
		$msg = GetMsgHelper::parseIni($file, $section, $process_sections);
		
		if($module) {
			$file = AppMsg::getModuleMsgFile($module, $file_name);
			if(file_exists($file)) {
				$msg = array_merge($msg, GetMsgHelper::parseIni($file, $section, $process_sections));					
			}
		}
		
		return $msg;			
	}
	
	
	function getMenuMsgs($module = false, $section = false, $process_sections = 1) {
		return AppMsg::getMsgs('menu_msg.ini', $module, $section, $process_sections);		
	}
	
	
	function getErrorMsgs($module = false, $section = false, $process_sections = 1) {
		return AppMsg::getMsgs('error_msg.ini', $module, $section, $process_sections);		
	}
	
	
	
/*
	function getMenuMsgs($module = false) {
		
		$file = AppMsg::getCommonMsgFile('menu_msg.ini');
		$msg = GetMsgHelper::parseIni($file);
		
		if($module) {
			$file = AppMsg::getModuleMsgFile($module, 'menu_msg.ini');
			if(file_exists($file)) {
				$msg = array_merge($msg, GetMsgHelper::parseIni($file));					
			}
		}
		
		return $msg;		
	}
	
	
	function getErrorMsgs($module = false) {
		
		$file = AppMsg::getCommonMsgFile('error_msg.ini');
		$msg = GetMsgHelper::parseIni($file);
		
		if($module) {
			$file = AppMsg::getModuleMsgFile($module, 'error_msg.ini');
			$msg = array_merge($msg, GetMsgHelper::parseIni($file));			
		}
		
		return $msg;
	}
*/
	
	
	// this after wrong form submission
	function errorBox(&$errors, $module = false) {
		
		if(!$errors) { return; }		

		$msg = AppMsg::getErrorMsgs($module);
		
		foreach($errors as $k => $v) {
			$e[] = $msg[$v['msg']];
		}
		
		$msgs['title'] = $msg['error_title_msg'];
		$msgs['body'] = '<li>' . implode('<li>', $e);
		return BoxMsg::factory('error', $msgs);
	}	
	
	
	
	// mostly it will generate hint in admin area if specified
	function hintBox($keyword, $module = false) {
		
		$file = ($module) ? AppMsg::getModuleMsgFile($module, 'hint_msg.ini')
		                  : AppMsg::getCommonMsgFile('hint_msg.ini');
		if(!file_exists($file)) { return; }

		
		$msg['body'] =& GetMsgHelper::parseMultiIni($file, $keyword);	
		if($msg['body']) {
			return BoxMsg::factory('hint', $msg);
		}
	}
	
	
	function wrongActionBox($keyword) {	
		
		$file = AppMsg::getCommonMsgFile('after_action_msg.ini');
		$msg =& GetMsgHelper::parseIni($file, $keyword, 1);
		
		if($msg) {
			return BoxMsg::factory('error', $msg);
		}
	}
	
	
	function afterActionBox($keyword, $factory = 'error', $module = false) {	
		
		$file = ($module) ? AppMsg::getModuleMsgFile($module, 'after_action_msg.ini')
		                  : AppMsg::getCommonMsgFile('after_action_msg.ini');
		if(!file_exists($file)) { return; }
		
		$msg =& GetMsgHelper::parseIni($file, $keyword, 1);
		
		if($msg) {
			return BoxMsg::factory($factory, $msg);
		}
	}
}
?>
